﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace DIProject
{	public partial class Form : System.Windows.Forms.Form
	{	public Form()
		{	InitializeComponent();
		}
//-------------------------------------------------------------------------------------------------
		private FormWindowState windowState = FormWindowState.Normal;
		protected override void OnShown(EventArgs e)
		{	if(!DesignMode)
				WindowState = windowState;
			base.OnShown(e);
		}
//-------------------------------------------------------------------------------------------------
		protected override void OnLoad(EventArgs e)
		{	base.OnLoad(e);
			if(!DesignMode)
				LoadParams();
		}
		protected override void OnFormClosed(FormClosedEventArgs e)
		{	SaveParams();
			base.OnFormClosed(e);
		}
//-------------------------------------------------------------------------------------------------
		protected XmlNode FormNode()
		{	return FormNode(false);
		}
		protected XmlNode FormNode(bool create)
		{	XmlNode formNode = null, formsNode;
			formsNode = Util.XmlIniFile.FindNode(Util.XmlIniFile.Document.DocumentElement, "forms", create);
			if(formsNode != null)
				formNode = Util.XmlIniFile.FindNode(formsNode, "form", this.Name, create);
			return formNode;
		}
//-------------------------------------------------------------------------------------------------
		protected virtual void LoadParams()
		{	XmlNode formNode = FormNode();
			if(formNode != null)
			{	switch(Util.XmlIniFile.GetInt(formNode, "windowState", -1))			// Смотри метод OnShown
				{	case -1:windowState = WindowState;					break;
					case 0:	windowState = FormWindowState.Normal;		break;
					case 1:	windowState = FormWindowState.Minimized;	break;
					case 2:	windowState = FormWindowState.Maximized;	break;
				}
			//?	if(windowState != FormWindowState.Maximized)
				{	Top		= Util.XmlIniFile.GetInt(formNode, "top",	Top		);
					Left	= Util.XmlIniFile.GetInt(formNode, "left",	Left	);
					switch(FormBorderStyle)
					{	case FormBorderStyle.Fixed3D:
						case FormBorderStyle.FixedDialog:
						case FormBorderStyle.FixedSingle:
						case FormBorderStyle.FixedToolWindow:
							break;	// пропускаем окна с фиксированными рамками
						default:
							Height	= Util.XmlIniFile.GetInt(formNode, "height",Height	);
							Width	= Util.XmlIniFile.GetInt(formNode, "width",	Width	);
							break;
					}
				}
				LoadParams(this, formNode);
			}
		}
		protected virtual void SaveParams()
		{	XmlNode formNode = FormNode(true);
			if(formNode != null)
			{	Util.XmlIniFile.PutInt(formNode, "top",		Top		);
				Util.XmlIniFile.PutInt(formNode, "left",	Left	);
				Util.XmlIniFile.PutInt(formNode, "height",	Height	);
				Util.XmlIniFile.PutInt(formNode, "width",	Width	);
				switch(WindowState)
				{	case FormWindowState.Normal:	Util.XmlIniFile.PutInt(formNode, "windowState", 0);	break;
					case FormWindowState.Minimized:	Util.XmlIniFile.PutInt(formNode, "windowState", 1);	break;
					case FormWindowState.Maximized:	Util.XmlIniFile.PutInt(formNode, "windowState", 2);	break;
				}
				SaveParams(this, formNode);
			}
		}
//-------------------------------------------------------------------------------------------------
		protected virtual void LoadParams(Control parentControl, XmlNode parentNode)
		{	Control control;
			XmlNode controlNode;
			Control.ControlCollection controls = parentControl.Controls;
			for(int i = 0, n = controls.Count; i < n; ++i)
			{	control = controls[i];
				controlNode = Util.XmlIniFile.FindNode(parentNode, control.GetType().Name, control.Name);
				if(controlNode != null)
				{	if(control is Splitter)
						LoadParams((Splitter)control, controlNode);
					else if(control is DataGridView)
						LoadParams((DataGridView)control, controlNode);
					else
						LoadParams(control, controlNode);
				}
			}
		}
		protected virtual void SaveParams(Control parentControl, XmlNode parentNode)
		{	Control control;
			XmlNode controlNode;
			Control.ControlCollection controls = parentControl.Controls;
			for(int i = 0, n = controls.Count; i < n; ++i)
			{	control = controls[i];
				controlNode = Util.XmlIniFile.FindNode(parentNode, control.GetType().Name, control.Name, true);
				if(controlNode != null)
				{	if(control is Splitter)
						SaveParams((Splitter)control, controlNode);
					else if(control is DataGridView)
						SaveParams((DataGridView)control, controlNode);
					else
						SaveParams(control, controlNode);
				}
			}
		}
//-------------------------------------------------------------------------------------------------
		protected virtual void LoadParams(Splitter splitter, XmlNode splitterNode)
		{	splitter.SplitPosition = Util.XmlIniFile.GetInt(splitterNode, "position", splitter.SplitPosition);
		}
		protected virtual void SaveParams(Splitter splitter, XmlNode splitterNode)
		{	Util.XmlIniFile.PutInt(splitterNode, "position", splitter.SplitPosition);
		}
//-------------------------------------------------------------------------------------------------
		protected virtual void LoadParams(DataGridView gridView, XmlNode gridViewNode)
		{	DataGridViewColumn column;
			XmlNode columnNode;
			DataGridViewColumnCollection columns = gridView.Columns;
			for(int i = 0, n = columns.Count; i < n; ++i)
			{	column = columns[i];
				columnNode = Util.XmlIniFile.FindNode(gridViewNode, "column", column.Name);
				if(columnNode != null)
					column.Width = Util.XmlIniFile.GetInt(columnNode, "width", column.Width);
			}
		}
		protected virtual void SaveParams(DataGridView gridView, XmlNode gridViewNode)
		{	DataGridViewColumn column;
			XmlNode columnNode;
			DataGridViewColumnCollection columns = gridView.Columns;
			Util.XmlIniFile.RemoveChildNodes(gridViewNode);
			for(int i = 0, n = columns.Count; i < n; ++i)
			{	column = columns[i];
				if(column.Visible)
				{	columnNode = Util.XmlIniFile.FindNode(gridViewNode, "column", column.Name, true);
					if(columnNode != null)
						Util.XmlIniFile.PutInt(columnNode, "width", column.Width);
				}
			}
		}
//-------------------------------------------------------------------------------------------------
	}
}